package com.example.common;

import java.util.AbstractQueue;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/*
@description:支持动态调整容量的阻塞队列 基于链表实现，通过锁机制保证线程安全
@ClassName ResizeLinkedBlockingQueue
@author chen
@create 2025-07-04 15:13
@Version 1.0
*/
public class ResizeLinkedBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, java.io.Serializable
{
    private static final long serialVersionUID = -6903933977591709194L;

    private volatile int capacity; // 队列容量（volatile保证可见性）

    private transient Node<E> last; // 队尾节点

    private transient Node<E> head; // 队头节点

    private final ReentrantLock putLock = new ReentrantLock(); // 入队锁

    private final AtomicInteger count = new AtomicInteger(); // 元

    // 节点结构
    static class Node<E>
    {
        E item;
        Node<E> next;
        Node(E x) {
            item = x;
        }
    }

    /**

     * 动态调整队列容量

     *
     * @param newCapacity 新容量

     * @throws IllegalArgumentException 新容量小于等于0或小于当前元素数量时抛出

     */
    public void setCapacity(int newCapacity)
    {
        if (newCapacity <= 0) throw new IllegalArgumentException("新容量必须大于0");
        final ReentrantLock lock = this.putLock;
        lock.lock();
        try {
            if (count.get() > capacity)
            {
                throw new IllegalArgumentException("新容量小于当前元素数量");
            }
            this.capacity = newCapacity;
        } finally {
            lock.unlock();
        }
    }

    /**

     * 构造方法

     * @param capacity 初始容量

     * @throws IllegalArgumentException 容量小于等于0时抛出

     */
    public ResizeLinkedBlockingQueue(int capacity)
    {
        if (capacity <= 0) throw new IllegalArgumentException();
        this.capacity = capacity;
        last = head = new Node<E>(null);
    }

    @Override
    public Iterator<E> iterator()
    {
        return null;
    }

    @Override
    public int size()
    {
        return 0;
    }

    @Override
    public void put(E e) throws InterruptedException
    {

    }

    @Override
    public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
    {
        return false;
    }

    @Override
    public E take() throws InterruptedException
    {
        return null;
    }

    @Override
    public E poll(long timeout, TimeUnit unit) throws InterruptedException
    {
        return null;
    }

    @Override
    public int remainingCapacity()
    {
        return 0;
    }

    @Override
    public int drainTo(Collection<? super E> c)
    {
        return 0;
    }

    @Override
    public int drainTo(Collection<? super E> c, int maxElements)
    {
        return 0;
    }

    @Override
    public boolean offer(E e)
    {
        return false;
    }

    @Override
    public E poll()
    {
        return null;
    }

    @Override
    public E peek()
    {
        return null;
    }
}
